// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "SET1Parser.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Partition/SET1Data.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Partition/ISET1Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SET1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;

// 以下需要修改
enum SET1Key : std::uint8_t
{
    SET1Key_ID = 2,
    SET1Key_CP = 3,  // 节点定义坐标系,默认为0
    SET1Key_X1 = 4,
    SET1Key_X2 = 5,
    SET1Key_X3 = 6,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSET1 (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                dividedEntryInfo)
{
    //

    FEM::SET1Data tempData;

    tempData.m_domainId = 1;

    size_t curIDKey = 1;  // 当前字符串是第几项

    tempData.m_id = convertToI64 (dividedEntryInfo[1]);

    bool lastTHRU = false;

    for (size_t i = 2; i < dividedEntryInfo.size (); i++)
    {
        std::string curStrKey = dividedEntryInfo[i];
        boost::trim (curStrKey);
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            continue;
        }

        if (boost::iequals (curStrKey, "THRU"))
        {
            lastTHRU = true;
            continue;
        }

        if (lastTHRU)
        {
            lastTHRU = false;
            tempData.m_gids.push_back (-convertToI64 (curStrKey));
        }
        else
        {
            tempData.m_gids.push_back (convertToI64 (curStrKey));
        }

        curIDKey++;
    }

    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<ISET1Service> ()->Add (tempData);
}